<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../common/EasyMysql.php");
require_once(dirname(__FILE__) . "/../config/ConfigDB.php");
/**
 * Description of AbnormalCom
 *
 * @author Administrator
 */
class AbnormalCom {
    //put your code here
    function __construct() {
        $dbconfig = $GLOBALS["config"] ['database'];
        $mysql = new EasyMysql($dbconfig['hostname'], $dbconfig ['port'], $dbconfig ['username'], $dbconfig ['password'], $dbconfig ['db'], $dbconfig ['charset']);
        $this->mysql = $mysql;
    }
    function addAbnormal($route_id,$user_id,$bus_id,$ow_id,$thing,$time,$reason){
        try{
            $sql = "insert into abnormal(route_id,bus_id,oneway_id,user_id,thing,create_time,reason) values({$route_id},{$bus_id},{$ow_id},{$user_id},{$thing},'{$time}','{$reason}')";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    function getAbnormalByStatus($status){
        try{
            $sql = "select * from abnormal where status=1";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0];
            }
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    function changeStatus($id,$status){
        try{
            $sql = "update abnormal set status={$status} where id={$id}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    function getNumber(){
        try{
            $sql = "select count(1) from abnormal";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            return false;
        }
        return 0;
    }
    function getAllAbnormal($minlimit,$count){
        try{
            $sql = "select * from abnormal order by create_time desc limit {$minlimit},{$count}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            return false;
        }
        return null;
    }
    function deleteAbnormal($id){
        try{
            $sql = "delete from abnormal where id={$id}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
    function getAbnormalByTime($mintime,$maxtime){
        try{
            $sql = "select * from abnormal where (create_time>{$mintime} and create_time<{$maxtime}) or create_time={$mintime}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0];
            }
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }
}
